package org.jeecg.modules.house.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.house.entity.BisHouse;
import org.jeecg.modules.house.mapper.BisHouseMapper;
import org.jeecg.modules.house.model.BisHouseVo;
import org.jeecg.modules.house.service.IBisHouseService;
import org.jeecg.modules.system.entity.SysFile;
import org.jeecg.modules.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;

/**
 * @Description: 房源实体
 * @author： jeecg-boot
 * @date： 2019-04-20
 * @version： V1.0
 */
@Service
@Slf4j
public class BisHouseServiceImpl extends ServiceImpl<BisHouseMapper, BisHouse> implements IBisHouseService {

    @Autowired
    private ISysFileService iSysFileService;

    @Override
    @Transactional
    public void saveNewBisHouse(@NotNull BisHouseVo bisHouseVo) throws Exception {
        BisHouse bisHouse = bisHouseVo.getBisHouse();
        if (!StringUtils.isEmpty(bisHouseVo.getCoverId())) {
            // 将封面图片保存至房源信息实体
            SysFile coverFile = iSysFileService.getById(bisHouseVo.getCoverId());
            bisHouse.setCover(coverFile.getRelativePath());
            // 保存封面图片文件
            SysFile sysFile = new SysFile();
            sysFile.setId(bisHouseVo.getCoverId());
            sysFile.setProductId(bisHouse.getId());
            sysFile.setType(CommonConstant.HOUSE_COVER);
            iSysFileService.updateById(sysFile);
        }
        if (!CollectionUtils.isEmpty(bisHouseVo.getSlideIds())) {
            for (String slideId : bisHouseVo.getSlideIds()) {
                SysFile item = new SysFile();
                item.setId(slideId);
                item.setProductId(bisHouse.getId());
                item.setType(CommonConstant.HOUSE_SLIDE);
                iSysFileService.updateById(item);
            }
        }
        this.save(bisHouse);
    }

    @Override
    @Transactional
    public void modifyBisHouse(@NotNull BisHouseVo bisHouseVo) throws Exception {
        BisHouse bisHouse = bisHouseVo.getBisHouse();
        // 更新时先清空对应的封面及轮播图文件
        iSysFileService.updateNullOnProductId(bisHouse.getId());
        // 重新更新封面关系
        if (!StringUtils.isEmpty(bisHouseVo.getCoverId())) {
            // 将封面图片保存至房源信息实体
            SysFile coverFile = iSysFileService.getById(bisHouseVo.getCoverId());
            bisHouse.setCover(coverFile.getRelativePath());
            // 保存封面图片文件
            SysFile sysFile = new SysFile();
            sysFile.setId(bisHouseVo.getCoverId());
            sysFile.setType(CommonConstant.HOUSE_COVER);
            sysFile.setProductId(bisHouse.getId());
            iSysFileService.updateById(sysFile);
        }
        // 重新更新轮播关系
        if (!CollectionUtils.isEmpty(bisHouseVo.getSlideIds())) {
            for (String slideId : bisHouseVo.getSlideIds()) {
                SysFile sysFile = new SysFile();
                sysFile.setId(slideId);
                sysFile.setType(CommonConstant.HOUSE_SLIDE);
                sysFile.setProductId(bisHouse.getId());
                iSysFileService.updateById(sysFile);
            }
        }
        this.updateById(bisHouse);
    }
}
